﻿namespace EyeOpen.Webcam.Mvc
{
	using System;
	using System.IO;
	using System.ServiceModel.Activation;
	using System.Web;

	[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]
	public class StreamService
		: IStreamService
	{
		public void Capture(byte[] value, DateTime dateTime)
		{
			var fileName =
				GetFileName(dateTime.Ticks.ToString());

			File.WriteAllBytes(fileName, value);
		}

		public void CaptureString(string value, DateTime dateTime)
		{
			var fileName =
				GetFileName(dateTime.Ticks.ToString());

			File.WriteAllText(fileName, value);
		}

		private static string GetFileName(string name)
		{
			var fileName = 
				string.Format(HttpContext.Current.Server.MapPath("~/App_Data/{0}.tga"), name);

			return fileName;
		}
	}
}